/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

package com.levelmaster2000
{
    import com.pblabs.engine.core.IPBObject;
    import com.pblabs.engine.core.PBGroup;
    import com.pblabs.engine.entity.IEntity;
    import com.pblabs.engine.PBE;
    import com.pblabs.rendering2D.IScene2D;
    import com.pblabs.rendering2D.ISpatialManager2D;
    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;
    import flash.utils.getQualifiedSuperclassName;

    public class EditorHelper
    {
        public static var editor:EditorMain = null;
        
        public static function getSuperClass(className:String):String
        {
            //var cl:Class = editor.app.getDefinition(className) as Class;
            //className = className.replace("::", ".");
            var def:Object = getDefinitionByName(className);
            var str:String = getQualifiedSuperclassName(def);
            return str;
            //return getQualifiedSuperclassName(getDefinitionByName(className));
        }
        
        //TODO: needed?
        public static function getClass(obj:Object):Class 
        {
            return Class(getDefinitionByName(getQualifiedClassName(obj)));
        }        
		
		public static function serializePBE():XML
		{
			var xml:XML = <things />;         
			
            var object:IPBObject = null;
			for each(object in PBE.nameManager.objectList)
			{
				if(object is IEntity)
				{			
					var entity:IEntity = object as IEntity;
                    
                    if (entity.owningGroup == editor.editorGroup
                        || entity.lookupComponentByType(IScene2D) != null
                        || entity.lookupComponentByType(ISpatialManager2D) != null)
                    {
                        continue;
                    }

                    entity.serialize(xml);
				}
			}
            
            //TODO: performance could be tweaked, if one puts the following code in the for loop above.
            //I just want all the groups to be defined at the end of the XML file.
            //TODO: add excludeGroups:Array param to group.serialize function.
            for each(object in PBE.nameManager.objectList)
            {
                if(object is PBGroup)
                {
                    var group:PBGroup = object as PBGroup;
                    
                    if(group == editor.editorGroup)
                    {
                        continue;
                    }
                    
                    var groupXML:XML = <group name={group.name} />

                    for(var i:int = 0;  i < group.length; i++)
                    {
                        if(group.getItem(i) is IEntity)
                        {
                            groupXML.appendChild(<objectReference name={group.getItem(i).name} />);
                        }
                        else if(group.getItem(i) is PBGroup)
                        {
                            if(group.getItem(i) == editor.editorGroup)
                            {
                                continue;
                            }
                            groupXML.appendChild(<groupReference name={group.getItem(i).name} />);
                        }
                    }
                    
                    xml.appendChild(groupXML);
                }
            }
            
			return xml;       
		}		
	}
}